package com.burgeon.backend.EnterpriseWeChat.utils;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class TestUrlUtils {
    public static void setListener(ConnectionListener listener) {
        connectListener = listener;
    }
//
    private static ConnectionListener connectListener;
//    public static boolean testUrl(String urlString,ConnectionListener listener){

    public static boolean testUrlWithTimeOut(String urlString){
        boolean isUse = false;
        int status = 404;
        try {
            URL urlObj = new URL(urlString);
            HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
            oc.setUseCaches(false);
            oc.setConnectTimeout(3000); // 设置超时时间
            status = oc.getResponseCode();// 请求状态

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("链接不可以");

        }
        if (200 == status) {
            // 200是请求地址顺利连通。。
            isUse = true;
            System.err.println("链接可以");
        }
        System.err.println("链接不可以");
        return isUse;
    }


    public static boolean testUrl(String urlStr){

        boolean isUse = false;
        int status = 404;
        try {
            URL urlObj = new URL(urlStr);
            HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
            oc.setUseCaches(false);
            oc.setConnectTimeout(5000); // 设置超时时间
            status = oc.getResponseCode();// 请求状态

        } catch (Exception e) {
            e.printStackTrace();

        }
        if (200 == status) {
            // 200是请求地址顺利连通。。
            isUse = true;
//            System.err.println("链接可以");
        }
//        System.err.println("链接不可以");
        return isUse;
    }


    public interface ConnectionListener{
        void onConnected();
        void onFailed();
    }
}
